# Importing Data
CHINA<- read_excel("C:/users/Biniam/Desktop/Documents/Academic/Thesis/Analysis Folder/Excel Files/Aluminium/Aluminium.xlsx",sheet = "Sheet1", range = "J1:J264")

# Checking the Imported Data
View(CHINA)
# Creating Time Series Data
CHINA_ts <- ts(CHINA, start=c(1998,1), end=c(2019,10), frequency=12)
# Viewing and Checking the Created Time Series Data
CHINA_ts
sum(is.na(CHINA_ts))
library(forecast)
CHINA_ts <- tsclean(CHINA_ts)
CHINA_ts

# Identification: Plotting the Time Series Data
plot(CHINA_ts, cex = 0.5, cex.main = 1.5, font.main = 4, font.lab = 2, cex.lab = 1.5, cex.axis = 0.1, col = 2, col.main = 4, col.lab = 2, col.axis = 3, xlab = "Years", ylab = "Volume of Aluminum Production (in tons)", main = "China's Monthly Aluminum Production")

# Estimating the appropriate model
CHINA_ts_model <- auto.arima(CHINA_ts)
CHINA_ts_model

plot.ts(CHINA_ts_model$resid, cex = 0.5, cex.main = 1.5, font.main = 4, font.lab = 2, cex.lab = 1.5, cex.axis = 0.75, col = 2, col.main = 4, col.lab = 2, col.axis = 4, xlab = "Years", ylab = "Residuals", main = "Residual Plot")
acf(CHINA_ts_model$residuals, cex = 0.5, cex.main = 1.5, font.main = 4, font.lab = 2, cex.lab = 1.5, cex.axis = 0.75, col = 2, col.main = 4, col.lab = 1, col.axis = 4, xlab = "Lag", ylab = "ACF", main="ACF Residual")
pacf(CHINA_ts_model$residuals, main='ACF Residual')
Box.test(CHINA_ts_model$resid, lag=20, type="Ljung-Box")


# Forecasting
options(max.print=1000000)
CHINA_ts_forecast <- forecast (CHINA_ts_model, level=c(95), h=254)
plot(CHINA_ts_forecast, cex = 0.5, cex.main = 1.5, font.main = 4, font.lab = 2, cex.lab = 1.5, cex.axis = 0.1, col = 2, col.main = 4, col.lab = 2, col.axis = 3, xlab = "Years", ylab = " Volume of Aluminum Production (in tons)", main = "Forecasted Monthly Aluminum Production for China")
CHINA_ts_forecast             

# Exporting
write.table(CHINA_ts_forecast, file="/users/Biniam/Desktop/Documents/Academic/Thesis/Result Folder/TSA Results/Excel Files/From R/Aluminium/CHINA_TSA.csv", sep=",")
